<template>
  <div>
    <div class="title">{{$t('run.desc_info')}}
      <!-- :disabled="stepObj.isAuto != 1 || phaseIsdis || isStep || isoperation == 1" -->
      <el-button type="primary" :disabled="isdisabled" size="small" style="float:right" :loading="loading"
        @click="handleSumit()">{{$t('run.submit')}}</el-button>
    </div>
    <el-form ref="form" :model="formData" label-width="0px" size="small">
      <el-row :gutter="24">
        <el-col :xs="24">
          <el-form-item label="">
            <el-input type="textarea" :rows="4" v-model="formData.remarks" placeholder="" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24">
          <div style="margin:15px 0;text-align:right">
            <el-checkbox v-model="formData.isError">{{$t('run.abnormal_information')}}</el-checkbox>
            <span class="_label">{{$t('run.comment_type')}}：</span>
            <el-select v-model="formData.noteType" placeholder="请选择" size="small" @change="changeSumitbStatu">
              <template v-for="item in options">
                <el-option :key="item.value" :label="item.label" :value="item.value" v-if="item.value != 2"></el-option>
              </template>

            </el-select>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div class="btnfang" style="text-align:center;margin-top:18px">
      <!-- phaseCode  TEM  温度控制暂时把暂停 启动 退出 禁止掉 -->
      <el-button :disabled="(stepObj.isActive != 1 || button1 || phaseIsdis) || isStep || isoperation == 1" type="primary" size="small"
        @click="handleEditPhaseStatus(10,'启动')">{{ $t("run.start_up") }}</el-button>
      <el-button :disabled="stepObj.isActive != 1 || button2 || phaseIsdis || isStep || isoperation == 1 || stepObj.phaseCode == 'PH_TEM'"
        size="small" @click="handleEditPhaseStatus(41,'停止')">{{ $t("run.stop") }}</el-button>
    </div>
  </div>
</template>

<script>
import { plc } from "@/api/plc"
export default {
  props: {
    stepObj: Object,
    count: Number,
    woId: String,
    phaseIsdis: Boolean,
    formsSeqNo: [String, Number],
    idObj: Object,
    isoperation: [Number, Object],
    statusFun: [String, Number],
    unitStat: Number,
    stateCd: [String, Number],
    woidData: String
  },
  data () {
    return {
      formData: {
        remarks: "",
        isError: "",
        noteType: "0"
      },
      options: [{
        label: this.$t('run.order_level'),
        value: "0"
      }, {
        label: this.$t('run.unit_level'),
        value: "1"
      }, {
        label: this.$t('run.phase_level'),
        value: "3"
      }, {
        label: this.$t('run.step_level'),
        value: "2"
      }
      ],
      loading: false,
      noteType: '',
      fid: ""
    }
  },
  watch: {
    count (val) {
      if (val) {
        this.formData.remarks = ""
        // this.formData.noteType = "0"
        this.formData.isError = false
        this.findHisComment()
        this.noteType = this.formData.noteType
        console.log(this.stepObj)
        console.log(this.statusFun)
      }
    }
  },
  computed: {
    isStep () {
      if (parseInt(this.stepObj.seqNo || 0) == this.formsSeqNo) {
        return false
      } else {
        return true
      }
    },
    isdisabled () {
      console.log(this.formData.noteType)
      console.log(this.stateCd)
      if (parseInt(this.formData.noteType) === 0) {
        if ((this.stateCd != 2 && this.stateCd != 4)) { //工单级别  工单为暂停 运行时可以录入
          return true
        } else {
          return false
        }
      } else if (this.formData.noteType == 1) { //单元级别  工单为暂停 运行时并且单元为暂停运行时可以录入
        if ((this.stateCd == 2 || this.stateCd == 4) && (this.statusFun == 10 || this.statusFun == 21)) {
          return false
        } else {
          return true
        }
      } else {
        if (this.stepObj.isActive != 1 || this.phaseIsdis || (parseInt(this.stepObj.seqNo || 0) != this.formsSeqNo) || this.isoperation == 1 || this.unitStat != 10) {
          return true
        } else {
          return false
        }
      }
    }, // 6温度控制   9蒸馏-氮气置换  10 蒸馏-冷凝接收
    button1 () { //启动按钮
      let arr = ['1', '2', '3', '5', '11', '12']
      let arr1 = ['0', '7', '8', '4', '6', '9', '10']
      if (this.unitStat === 0 || this.unitStat == 11) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else if (arr1.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else if (this.unitStat == 51) {
        let arr2 = ['5', '1', '2', '3', '11', '12']
        if (arr2.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    button2 () { //停止按钮
      let arr = ['5', '1', '2', '3', '11', '12']
      if (this.unitStat == 10) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    }
  },
  created () {

  },
  methods: {
    findHisComment () {
      this.fid = ""
      if (this.woidData) {
        plc.findHisCommentByWoId(this.woidData).then(res => {
          if (res.code == 200 && res.data && res.data.length) {
            this.formData.noteType = res.data[0].level + ""
            this.formData.isError = res.data[0].type == 1 ? true : false
            this.formData.remarks = res.data[0].flastModifyComment
            this.fid = res.data[0].fid
          }
        })
      } else {
        plc.findHisComment(this.stepObj.fid).then(res => {
          if (res.code == 200) {
            this.formData.noteType = res.data.level + ""
            this.formData.isError = res.data.type == 1 ? true : false
            this.formData.remarks = res.data.flastModifyComment
            this.fid = res.data.fid
          }
        })
      }

    },
    handleSumit () {
      console.log(this.stepObj.fid)
      if (!this.stepObj.operationId && this.formData.noteType == 3) {
        this.$message.error('请选择阶段');
        return
      }
      if (!this.formData.remarks) {
        this.$message.error('请输入描述信息');
        return
      }
      let param = {
        type: this.formData.isError == false ? 0 : 1,
        flastModifyComment: this.formData.remarks,
        level: this.formData.noteType,
        // unitId:this.idObj.opId,
        unitId: this.idObj.equipmentId,
        woId: this.woId,
        phaseId: this.stepObj.fid,
        jobId: this.idObj.jobId,
        fid: this.fid
      }
      console.log(param)
      this.loading = true
      plc.submitDescribeCommand(param).then(res => {
        this.loading = false
        if (res.code == 200) {
          this.$message({ duration: 1000, type: 'success', message: '提交成功!' });
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    changeSumitbStatu (val) {
      console.log(this.unitStat)
      // if(parseInt(val) === 0){
      //     this.isdisabled =  false
      // }else if(val == 1){
      //     if(this.unitStat == 10){ //工单运行
      //         console.log('sdsd')
      //         this.isdisabled =  false
      //     }else{
      //         console.log(545454)
      //        this.isdisabled =  true
      //     }
      // }else{
      //     if(this.stepObj.isAuto != 1 || this.phaseIsdis || (parseInt(this.stepObj.seqNo || 0) == this.formsSeqNo) || this.isoperation == 1){
      //         console.log(123)
      //         this.isdisabled =  true
      //     }else{
      //         console.log(111)
      //         this.isdisabled =  false
      //     }
      // }
    },
    handleEditPhaseStatus (type, txt) {
      if (!this.stepObj.fid) {
        this.$message.error('请选择阶段');
        return
      }
      let that = this
      this.$customConfirm({
        content: '是否要' + txt + '此阶段',
        onok () {
          const param = {
            fid: that.stepObj.fid,
            stateCd: type
          }
          plc.sendPlcCommandPhase(param).then(res => {
            if (res.code == 200) {
              that.$message({ duration: 1000, type: 'success', message: txt + '成功!' });
              if (type == 40 && that.stepObj.serialNo == 7) {
                that.submitDescribeCommand()
              }

            }
          })
        },
      })
    },

  }
}
</script>
<style scoped>
.title {
  margin-bottom: 15px;
}
._label {
  font-size: 14px;
  line-height: 19px;
  color: #606266;
  margin-left: 25px;
}
</style>